// pages/index/register.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    need_login:'yes'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;

      wx.request({
        url: getApp().data.servser + '/parking/list',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        success: function (res) {
          for (var i = 0; i < res.data.data.parkingInfos.length; i++) {
            res.data.data.parkingInfos[i].distance = Math.trunc(that.juli(res.data.data.parkingInfos[i].latitude, res.data.data.parkingInfos[i].longitude, app.globalData.latitude, app.globalData.longitude)) + '公里'
          }
          that.setData({
            need_login: 'no',
            parkingInfos: res.data.data.parkingInfos
          })
        }
      })
    
  },
  juli(lat1, lng1, lat2, lng2) {
    console.log(lat1 + " " + lng1)
    console.log(lat2 + " " + lng2)
    var radLat1 = lat1 * Math.PI / 180.0;
    var radLat2 = lat2 * Math.PI / 180.0;
    var a = radLat1 - radLat2;
    var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * 6378.137;
    s = Math.round(s * 10000) / 10000;
    return s
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  getInputUserId: function(e) {
    this.data.username = e.detail.value;
  },
  getInputPwd: function(e) {
    this.data.password = e.detail.value;
  },
  login: function() {
    let that = this;
    wx.request({
      url: getApp().data.servser + '/auth',
      method: 'GET',
      data: {
        username: that.data.username,
        password: that.data.password
      },
      success(res) {
        if (res.data.code == 500) {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
          // 登录失败
          that.setData({
            need_login: 'yes'
          })
        } else {
          wx.setStorageSync('username', that.data.username);
          wx.setStorageSync('password', that.data.password);
          wx.request({
            url: getApp().data.servser + '/parking/list',
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'Authorization': 'Bearer ' + wx.getStorageSync('token')
            },
            success: function(res) {
              for (var i = 0; i < res.data.data.parkingInfos.length; i++) {
                res.data.data.parkingInfos[i].distance = Math.trunc(that.juli(res.data.data.parkingInfos[i].latitude, res.data.data.parkingInfos[i].longitude, app.globalData.latitude, app.globalData.longitude)) + '公里'
              }
              that.setData({
                need_login: 'no',
                parkingInfos: res.data.data.parkingInfos
              })
            }
          })
        }
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})